package com.ohuang.apkMerge

import sun.plugin2.jvm.RemoteJVMLauncher.CallBack
import java.io.File
import java.lang.StringBuilder

/***
 * 合并反编译后的apk
 * 从path合并到path2
 */
fun mergeApk(path: String, path2: String,isNewId: Boolean=false) {
    var currentTimeMillis = System.currentTimeMillis()
    var sb = StringBuilder()
    println("---开始复制res---")
    File("$path/res").listFiles()?.forEach {
        val targetPath = "$path2/res"
        if (it.isDirectory) {
            println("--------开始复制${it.name}-------")
            if (it.name.startsWith("values")) {
                mergeValueXml(it.absolutePath, targetPath + "/${it.name}", isNewID = isNewId)
            } else {
                copyRes(it.absolutePath, targetPath + "/${it.name}", callBack = {a,b->
//                    sb.append("文件重名: 不复制文件  $a 和 $b 文件名重复 ")
//                    sb.append("\n")
                })
            }

        }
    }
    println("---开始复制lib---")
    copyPathAllFile("$path/lib", "$path2/lib")
    println("---开始复制assets---")
    copyPathAllFile("$path/assets", "$path2/assets")
    println("---开始复制kotlin---")
    copyPathAllFile("$path/kotlin", "$path2/kotlin")
    println("---开始复制smali和修改R.smali----")
    copySmaliAndUpdaterSmali(path, path2, "$path2/res/values/public.xml")
    println("---合并AndroidManifest.xml---")
    mergeManifest("$path/AndroidManifest.xml", "$path2/AndroidManifest.xml")
//    println("---输出日志文件$path2/log.txt----")
//    writeText(File("$path2/log.txt"),sb.toString())
    println("合并完成 用时：" + (System.currentTimeMillis() - currentTimeMillis) / 1000 + "s")


}

/**
 * 复制文件
 * callBack（） 已存在文件 不复制回调
 */
fun copyRes(
    sourceRootPath: String,
    targetRootPath: String,
    callBack: (sourcePath: String, targetPath: String) -> Unit = { _, _ -> }
) {
    fun fileName2NoSuffix(string: String): String = string.split(".")[0]

    var targetFile = File(targetRootPath)
    var sourceRootFile = File(sourceRootPath)
    var hashMap = HashMap<String, String>()
    targetFile.listFiles()?.forEach {
        if (it.isFile) {
            hashMap[fileName2NoSuffix(it.name)] = it.absolutePath
        }
    }

    sourceRootFile.listFiles()?.forEach {
        if (it.isFile) {
            var key =fileName2NoSuffix(it.name)
            if (hashMap.containsKey(key)){
                callBack.invoke(it.absolutePath,hashMap[key]!!)
            }else{
                copyFile(it.absolutePath,targetRootPath+"/${it.name}")
            }

        }
    }

}


/***
 * 强合并(文件重名直接覆盖)
 * 合并反编译后的apk
 * 从path合并到path2
 */
fun mergeApk2(path: String, path2: String,isNewId: Boolean=false) {
    var currentTimeMillis = System.currentTimeMillis()
    println("---开始复制res---")
    File("$path/res").listFiles()?.forEach {
        val targetPath = "$path2/res"
        if (it.isDirectory) {
            println("--------开始复制${it.name}-------")
            if (it.name.startsWith("values")) {
                mergeValueXml(it.absolutePath, targetPath + "/${it.name}",isNewID = isNewId)
            } else if (it.name.startsWith("xml")) {
                copyPathAllFile(it.absolutePath, targetPath + "/${it.name}", isCover = true)
            } else {
                copyPathAllFile(it.absolutePath, targetPath + "/${it.name}", isCover = true)
            }

        }
    }
    println("---开始复制lib---")
    copyPathAllFile("$path/lib", "$path2/lib", isCover = true)
    println("---开始复制assets---")
    copyPathAllFile("$path/assets", "$path2/assets", isCover = true)
    println("---开始复制kotlin---")
    copyPathAllFile("$path/kotlin", "$path2/kotlin", isCover = true)
    println("---开始复制smali和修改R.smali----")
    copySmaliAndUpdaterSmali(path, path2, "$path2/res/values/public.xml")
    println("---合并AndroidManifest.xml---")
    mergeManifest("$path/AndroidManifest.xml", "$path2/AndroidManifest.xml")
    println("合并完成 用时：" + (System.currentTimeMillis() - currentTimeMillis) / 1000 + "s")

}